//请求使用
import axios from 'axios'
import VueAxios from 'vue-axios'
import Vue from 'vue'
import ElementUI, {Loading} from 'element-ui';
import QS from "qs";

let loading; //定义loading变量

function startLoading(text) { //使用Element loading-start 方法
    let loadingText = "请稍等...";
    if (text) {
        loadingText = text + '...';
    }
    loading = Loading.service({
        lock: true,
        text: loadingText,
        background: 'rgba(0, 0, 0, 0.2)'
    })
}

function endLoading() { //使用Element loading-close 方法
    loading.close()
}

let needLoadingRequestCount = 0

export function showFullScreenLoading(text) {
    if (needLoadingRequestCount === 0) {
        startLoading(text);
    }
    needLoadingRequestCount++
}

export function tryHideFullScreenLoading() {
    if (needLoadingRequestCount <= 0) return
    needLoadingRequestCount--
    if (needLoadingRequestCount === 0) {
        endLoading()
    }
}

//axios配置和处理
Vue.use(VueAxios, axios);
// axios.defaults.baseURL = '/axios';//项目的的基础url
axios.defaults.baseURL = "http://localhost:9001"
axios.defaults.headers.common['Authorization'] = "";//请求token信息配置
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';//请求type设置
axios.defaults.timeout = 30000;//在超时前，所有请求都会等待30秒
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前处理
    // config.headers.common['Authorization'] = localStorage.getItem('Authorization');//判断token在前台session中是否存在
    //请求loading处理
    if (config.data) {
        let needLoading = config.data.needLoading;
        let loadingText = config.data.loadingText;
        if (needLoading != false) {
            showFullScreenLoading(loadingText);
        }
        //清除掉不需要的参数
        delete config.data["needLoading"];
        delete config.data["loadingText"];
    }
    /*
    * 这段代码解决添加loading 只能再data中添加needLoading和loadingText
    * 可以在axios中直接添加needLoading和loadingText,这两个参数和method,url同级
    * */
    let needLoading = config.needLoading;
    let loadingText = config.loadingText;
    if (needLoading == true) {
        showFullScreenLoading(loadingText);
    }
    //清除掉不需要的参数
    delete config["needLoading"];
    delete config["loadingText"];
    // console.log(config)
    // config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    if(config.method=="post"){
        config.data = QS.stringify(config.data);
    }
    return config;
}, function (error) {
    // 对请求错误做处理
    return Promise.reject(error);
});
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据处理
    tryHideFullScreenLoading();
    if (response && (response.status == 200)) {
        if (response.data.exceptionMes) {
            ElementUI.MessageBox({
                type: 'info',
                title: response.data.exceptionMes,
                message: response.data.debugMes
            });
        }
        if (response.headers["content-type"] == "text/html") {
            ElementUI.MessageBox({
                type: 'error',
                title: "提示",
                message: response.request.responseURL
            });
            // window.location.href = response.request.responseURL
        } else {
            if(response.data.result!=200&&response.config.noEroMsg!=true){
                ElementUI.MessageBox({
                    type: 'error',
                    title: "提示",
                    message: response.data.msg
                });
            }
            return response.data;
        }
    }
    return response;
}, function (error) {
    tryHideFullScreenLoading();
    // 对响应错误处理
    return Promise.reject(error);
});
Vue.prototype.$axios = axios;//定义调用方式

export default axios;

